<?php

namespace App\Classes\Injection;

use App\Models\Entity\Asset\Video\Vseriali;
use DB;
use Storage;

/**
 * 媒资(分集)注入自定义类
 */
class VserialiInjection
{
    /**
     * [$injectionWhere 注入的条件]
     * @var array
     */
    protected $injectionWhere = ['online_status' => 1];

    /**
     * [$injectVserialiHistory description]
     * @var [type]
     */
    protected $injectVserialiHistory;

    /**
     * [$ids description]
     * @var [type]
     */
    protected $ids;

    /**
     * { 判断是单个剧集还是多个剧集 }
     *
     * @var        boolean
     */
    public $flag = false;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($injectVserialiHistory, $ids = null)
    {
        $this->injectVserialiHistory = $injectVserialiHistory;
        $this->ids                  = $ids;
        if (!is_null($ids)) {
            if (is_array($ids)) {
                $this->injectionWhere = ['online_status' => 1, 'publish_status' => 0];
                $this->flag = true;
            } else {
                $this->injectionWhere = ['online_status' => 1, 'id' => $ids, 'publish_status' => 0];
            }
        } 
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 开始时间
        $starttime = microtime(true);
        // 开始使用内存
        $startMemory = memory_get_usage();

        /**
         * 生成xml, 对上线的媒资进行注入
         */
        // 生成xml
        $uploadConfig = config('custom.base.upload.xml');
        $driver       = $uploadConfig['driver'];
        $dir          = $uploadConfig['dir'];
        $rootDir      = config('filesystems.disks.' . $driver . '.upload');

        // 完整路径
        $fullPath = $rootDir . '/' . $dir . '/';

        // 注入媒资总数
        $injectionVasseticount = 0;
        // 错误的行数
        $errorRow = [];

        /**
         * 配值
         */
        // ftp配置
        $ftp_config = config('ftp.connections.connection1');
        // 分块
        $injection_chunk_num = config('custom.base.job.vseriali.injection_chunk_num');
        // cp_id
        $config_media_injection_cp_id = config('custom.base.media_injection.cp_id');
        // url
        $config_media_injection_url = config('custom.base.media_injection.url');

        /**
         * xml注入, 500条一次处理
         */
        if ($this->flag) {
            $vserialis = Vseriali::where($this->injectionWhere)->whereIn('id', $this->ids)->with(['vasseti', 'vasseti.vcat', 'vasseti.actors', 'vasseti.directors', 'vasseti.vareas', 'vasseti.vlangs', 'vasseti.vyears', 'vasseti.vcattags'])->get();
        } else {
            $vserialis = Vseriali::where($this->injectionWhere)->with(['vasseti', 'vasseti.vcat', 'vasseti.actors', 'vasseti.directors', 'vasseti.vareas', 'vasseti.vlangs', 'vasseti.vyears', 'vasseti.vcattags'])->get();
        }
        // Vseriali::where($this->injectionWhere)->with(['vasseti', 'vasseti.vcat', 'vasseti.actors', 'vasseti.directors', 'vasseti.vareas', 'vasseti.vlangs', 'vasseti.vyears', 'vasseti.vcattags'])->chunk($injection_chunk_num, function ($vserialis) use ($driver, $fullPath, $ftp_config, $config_media_injection_cp_id, $config_media_injection_url, &$injectionVasseticount, &$errorRow) {
            foreach ($vserialis as $vseriali) {
                
                // 更新注入状态为正在注入
                Vseriali::where('id', '=', $vseriali->id)->update(['async_result' => 0]);
                
                // 完成数+1
                $this->injectVserialiHistory->update([
                    'done_count' => DB::raw("done_count + 1"),
                ]);
                // 如果分集关联不到剧集, 则说明有问题
                if (!$vseriali->vasseti) {
                    // 错误的媒资id
                    $errorRow[] = $vseriali_id;
                }

                /**
                 * 数据准备
                 */
                $vseriali_id = $vseriali->id;
                // 属性名:ElementType;取值:Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息;必填:Y;
                $elementType = 'Series';
                // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                $id = 'HDTYJJ' . str_pad($vseriali->vasseti->id, 26, "0", STR_PAD_LEFT);
                // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                $action = 'REGIST';
                // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                $code = $id;
                // 属性名:OnlineIdentify(第三方上线标识);必填:N;类型长度:string;自动上线规则配置;
                $onlineIdentify = config('custom.base.media_injection.free_flag');
                // 节点名:Name(连续剧名称);必填:Y;类型长度:128;
                $name = $vseriali->vasseti->name;
                // 节点名:OrderNumber(节目订购编号);必填:N;类型长度:string,10;
                $orderNumber = '';
                // 节点名:OriginalName(原名);必填:N;类型长度:string,128;
                $originalName = '';
                // 节点名:Sort(剧集排序规则);取值:0=>降序, 1=>升序;必填:Y;类型长度:string,128;
                $sort = '';
                // 节点名:KeyWords(搜索简拼);必填:N;类型长度:string,128;
                $keyWords = '';
                // 节点名:ActorDisplay(演员列表);必填:Y;类型长度:string,1024;多个之间用 `/` 分割;
                $actors = '';
                if ($actors = $vseriali->vasseti->actors->pluck('name')) {
                    // $actors = implode('/', $actors->toArray());
                    $actors = $vseriali->vasseti->actors->implode('/');
                }
                // 节点名:WriterDisplay(演员列表);必填:Y;类型长度:string,256;多个之间用 `/` 分割;
                $directors = '';
                if ($vseriali->vasseti->directors) {
                    $directors = implode('/', array_column($vseriali->vasseti->directors->toArray(), 'name'));
                }
                // 节点名:OriginalCountry(国家地区);必填:Y;类型长度:string,64;多个之间用 `/` 分割;
                $vareas = '';
                if ($vareas = $vseriali->vasseti->vareas->pluck('name')) {
                    $vareas = implode('/', $vareas->toArray());
                }
                // 节点名:ReleaseYear(上映年份YYYY-m-d);必填:Y;类型长度:int,4;
                // $vasseti->vasseti->vyears是年代标签
                $vyears = date("Y-m-d", strtotime($vseriali->vasseti->firstdate));
                // 节点名:OrgairDate(首播日期YYYYMMDD);必填:Y;类型长度:string,8;
                $orgairDate = date("Ymd", strtotime($vseriali->vasseti->firstdate));
                // 节点名:LicensingWindowStart(有效开始时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowStart = '';
                // 节点名:LicensingWindowEnd(有效结束时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowEnd = '';
                // 节点名:Macrovision(拷贝保护标志);取值:0=>无拷贝保护,1=>有拷贝保护;必填:Y;类型长度:int,1;默认:0;
                $macrovision = 0;
                // 节点名:Description(节目描述);必填:Y;类型长度:string,2048;
                $description = $vseriali->vasseti->short_desc;
                // 节点名:PriceTaxIn(列表定价);必填:N;类型长度:float,12,2;
                $priceTaxIn = '';
                // 节点名:Status(节目状态标志);必填:N;类型长度:int,1;默认:1;
                $status = 1;
                // 节点名:SourceType;取值:1=>VDO,5=>Advertisment必填:Y;类型长度:int,1;默认:1;
                $sourceType = 1;
                // 节点名:Type(节目内容类型);必填:Y;类型长度:int,2;字符串,表示内容类型
                $type = $vseriali->vasseti->vcat->name;
                // 节点名:Keywords(关键字);必填:Y;类型长度:string,1024;多个关键字使用/分隔;
                $keywords = '';
                // 节点名:Tags(标签集合);必填:Y;类型长度:string,1024;多个标签使用/分隔;
                $tags = '';
                if ($tags = $vseriali->vasseti->vcattags->pluck('name')) {
                    $tags = $tags->implode('/');
                }
                // 节点名:ContentProvider(内容提供商);必填:N;类型长度:string,64;
                $contentProvider = '';
                // 节点名:Duration(节目时长);必填:Y;类型长度:int,3;以分钟为单位;
                $duration = '';

                // 节点名:PlayCount(播放次数);必填:N;类型长度:int,6;
                $playCounts = 0;
                // 节点名:Score(评分);必填:N;类型长度:int,6;
                $score = $vseriali->vasseti->score;
                // 节点名:VolumnCount(总集数);必填:Y;类型长度:int,4;
                // 如果是电视剧就是总集数【如果有分集就是电视剧】, 否则就是1
                $volumnCount = 1;
                if ($vseriali->vasseti->vserialis) {
                    $volumnCount = $vseriali->vasseti->episode;
                }
                // 节点名:ShowType(剧集展示规则);取值:0=>集数,1=>海报+日期,2=>标题+简介,3=>看点+集数;必填:Y;类型长度:int,2;
                $showType = '';
                // 节点名:NewCount(最新集数);必填:Y;类型长度:int,4;
                $newCount = '';
                // 别名
                $aliasName = '';
                // 英文名
                $englishName = '';
                // 节点名:ParentCategoryName(父级栏目名称);必填:N;类型长度:String,64;如果是二级栏目请传入父级栏目名称
                $parentCategoryName = '';
                // 父类ID
                $parentCategoryId = '';
                // 类型ID
                $typeId = '';
                // 节点名:Language(语言);必填:Y;类型长度:string,64;多个之间用 `/` 分割;
                $vlangs = '';
                if ($vlangs = $vseriali->vasseti->vlangs->pluck('name')) {
                    $vlangs = $vlangs->implode('/');
                }
                // 种类
                $kind = $type;
                // 节点名:CPContentID(CP标识);必填:Y;类型长度:String,128;
                $cpContentID = '';

                /**
                 * xml生成
                 */
                // 创建version标签$version=$doc->createElement("version");$versionTextNode=$doc->createTextNode("1.0");$version->appendchild($versionTextNode);当<root>标签中有值时就可以显示完整的<root>标签了
                // header("Content-type: text/xml"); //头文件非常重要
                // 创建xml文件, 建立DOM对象
                $dom = new \DOMDocument('1.0', 'utf-8');

                // 格式化输出，友好地保留缩进和空格
                // $doc->formatOutput = true;

                // 建立<ADI>元素
                $adi = $dom->createElement('ADI');
                // 添加属性
                $adi->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');

                // 把<ADI>元素追加到文档里面
                $dom->appendChild($adi);

                // 建立<Objects>元素并将其作为<ADI>的子元素
                $objects = $dom->createElement('Objects');
                $adi->appendChild($objects);

                /**
                 * ElementType="Series" start
                 */
                // 单个<Object>
                $object = $dom->createElement('Object');
                // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                // $no3 = $dom->createAttribute('id');//创建属性节点：id
                // $no3->value = 1;//给属性节点赋值
                // $no2->appendChild($no3);//把属性节点加入到book节点中
                $object->setAttribute('ElementType', $elementType);
                // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                // 唯一ID, 必须
                $object->setAttribute('ID', $id);
                // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                $object->setAttribute('Action', $action);
                // 全局唯一标志, 必须
                $object->setAttribute('Code', $code);
                $objects->appendChild($object);
                
                // 第三方上线标识<Property>
                $property = $dom->createElement('Property');
                // 第三方上线标识, 非必须
                $property->setAttribute('Name', 'OnlineIdentify');
                $text = $dom->createTextNode($onlineIdentify);
                $property->appendChild($text);
                $object->appendChild($property);

                // 视频名称<Property>
                $property = $dom->createElement('Property');
                // 视频名称, 必须
                $property->setAttribute('Name', 'Name');
                $text = $dom->createTextNode($name);
                $property->appendChild($text);
                $object->appendChild($property);

                // 节目订购编号<Property>
                $property = $dom->createElement('Property');
                // 节目订购编号, 非必须
                $property->setAttribute('Name', 'OrderNumber');
                $text = $dom->createTextNode($orderNumber);
                $property->appendChild($text);
                $object->appendChild($property);

                // 原名<Property>
                $property = $dom->createElement('Property');
                // 原名, 非必须
                $property->setAttribute('Name', 'OriginalName');
                $text = $dom->createTextNode($originalName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 剧集排序规则<Property>
                $property = $dom->createElement('Property');
                // 剧集排序规则, 0降序, 1升序, 必须
                $property->setAttribute('Name', 'Sort');
                $text = $dom->createTextNode($sort);
                $property->appendChild($text);
                $object->appendChild($property);

                // 搜索简拼<Property>
                $property = $dom->createElement('Property');
                // 搜索简拼, 非必须
                $property->setAttribute('Name', 'KeyWords');
                $text = $dom->createTextNode($keyWords);
                $property->appendChild($text);
                $object->appendChild($property);

                // 演员列表<Property>
                $property = $dom->createElement('Property');
                // 演员列表, 必须
                $property->setAttribute('Name', 'ActorDisplay');
                $text = $dom->createTextNode($actors);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 导演列表, 必须
                $property->setAttribute('Name', 'WriterDisplay');
                $text = $dom->createTextNode($directors);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 国家地区, 必须
                $property->setAttribute('Name', 'OriginalCountry');
                $text = $dom->createTextNode($vareas);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 上映年份, 必须
                $property->setAttribute('Name', 'ReleaseYear');
                $text = $dom->createTextNode($vyears);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 首播日期, 必须
                $property->setAttribute('Name', 'OrgAirDate');
                $text = $dom->createTextNode($orgairDate);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效开始时间, 必须
                $property->setAttribute('Name', 'LicensingWindowStart');
                $text = $dom->createTextNode($licensingWindowStart);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效结束时间, 必须
                $property->setAttribute('Name', 'LicensingWindowEnd');
                $text = $dom->createTextNode($licensingWindowEnd);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 拷贝保护标志, 必须
                $property->setAttribute('Name', 'Macrovision');
                $text = $dom->createTextNode($macrovision);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目描述, 必须
                $property->setAttribute('Name', 'Description');
                $text = $dom->createTextNode($description);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 列表定价, 不必须
                $property->setAttribute('Name', 'PriceTaxIn');
                $text = $dom->createTextNode($priceTaxIn);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目状态标志, 必须
                $property->setAttribute('Name', 'Status');
                $text = $dom->createTextNode($status);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // VOD/Adverisement, 必须
                $property->setAttribute('Name', 'SourceType');
                $text = $dom->createTextNode($sourceType);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目内容类型, 必须
                $property->setAttribute('Name', 'Type');
                $text = $dom->createTextNode($type);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 关键字, 多个关键字直接用/
                $property->setAttribute('Name', 'Keywords');
                $text = $dom->createTextNode($keywords);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 标签集合, 必须
                $property->setAttribute('Name', 'Tags');
                $text = $dom->createTextNode($tags);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 内容提供商, 非必须
                $property->setAttribute('Name', 'ContentProvider');
                $text = $dom->createTextNode($contentProvider);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目时长, 必须
                $property->setAttribute('Name', 'Duration');
                $text = $dom->createTextNode($duration);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 播放次数, 非必须
                $property->setAttribute('Name', 'PlayCount');
                $text = $dom->createTextNode($playCounts);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 评分, 非必须
                $property->setAttribute('Name', 'Score');
                $text = $dom->createTextNode($score);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 总集数, 必须
                $property->setAttribute('Name', 'VolumnCount');
                $text = $dom->createTextNode($volumnCount);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 剧集展示规则, 必须
                $property->setAttribute('Name', 'ShowType');
                $text = $dom->createTextNode($showType);
                $property->appendChild($text);
                $object->appendChild($property);

                // 最新集数<Property>
                $property = $dom->createElement('Property');
                // 最新集数, 必须
                $property->setAttribute('Name', 'NewCount');
                $text = $dom->createTextNode($newCount);
                $property->appendChild($text);
                $object->appendChild($property);

                // AliasName<Property>
                $property = $dom->createElement('Property');
                // 别名, 必须
                $property->setAttribute('Name', 'AliasName');
                $text = $dom->createTextNode($aliasName);
                $property->appendChild($text);
                $object->appendChild($property);

                // EnglishName<Property>
                $property = $dom->createElement('Property');
                // 英文名, 必须
                $property->setAttribute('Name', 'EnglishName');
                $text = $dom->createTextNode($englishName);
                $property->appendChild($text);
                $object->appendChild($property);

                // ParentCategoryName<Property>
                $property = $dom->createElement('Property');
                // 父类类型名字, 必须
                $property->setAttribute('Name', 'ParentCategoryName');
                $text = $dom->createTextNode($parentCategoryName);
                $property->appendChild($text);
                $object->appendChild($property);

                // ParentCategoryId<Property>
                $property = $dom->createElement('Property');
                // 父类类型ID, 必须
                $property->setAttribute('Name', 'ParentCategoryId');
                $text = $dom->createTextNode($parentCategoryId);
                $property->appendChild($text);
                $object->appendChild($property);

                // TypeId<Property>
                $property = $dom->createElement('Property');
                // 类型ID, 必须
                $property->setAttribute('Name', 'TypeId');
                $text = $dom->createTextNode($typeId);
                $property->appendChild($text);
                $object->appendChild($property);

                // 语种-Language<Property>
                $property = $dom->createElement('Property');
                // 语言, 非必须
                $property->setAttribute('Name', 'Language');
                $text = $dom->createTextNode($vlangs);
                $property->appendChild($text);
                $object->appendChild($property);

                // 种类-Kind<Property>
                $property = $dom->createElement('Property');
                // 种类, 必须
                $property->setAttribute('Name', 'Kind');
                $text = $dom->createTextNode($kind);
                $property->appendChild($text);
                $object->appendChild($property);
                /**
                 * ElementType="Series" end
                 */

                /**
                 * ElementType="Program" start
                 */
                // Program-节目信息
                $elementType = 'Program';
                // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                $program_id = str_pad(makeRandomNumber(32), 32, "0", STR_PAD_LEFT); // uniqid() . udate('YmdHisu')
                // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                $action = 'REGIST';
                // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                $program_code = $program_id;
                // 节点名:Name(连续剧名称);必填:Y;类型长度:128;
                $name = $vseriali->name;
                // 节点名:OrderNumber(节目订购编号);必填:N;类型长度:string,10;
                $orderNumber = '';
                // 节点名:OriginalName(原名);必填:N;类型长度:string,128;
                $originalName = '';
                // 节点名:SortName(索引名称供界面排序);必填:N;类型长度:string,128;
                $sortName = '';
                // 节点名:KeyWords(搜索简拼);必填:N;类型长度:string,128;
                $keyWords = '';
                // 节点名:ActorDisplay(演员列表);必填:Y;类型长度:string,1024;多个之间用 `/` 分割;
                $actors = '';
                // 节点名:WriterDisplay(演员列表);必填:Y;类型长度:string,256;多个之间用 `/` 分割;
                $directors = '';
                // 国家
                $vareas = '';
                // 上映时间
                $vyears = '';
                // 首播时间
                $orgairDate = '';
                // 节点名:Language(语言);必填:Y;类型长度:string,64;多个之间用 `/` 分割;
                $vlangs = '';
                // 节点名:LicensingWindowStart(有效开始时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowStart = '';
                // 节点名:LicensingWindowEnd(有效结束时间YYYYMMDDHH24MiSS);必填:Y;类型长度:string,14;
                $licensingWindowEnd = '';
                // 节点名:Macrovision(拷贝保护标志);取值:0=>无拷贝保护,1=>有拷贝保护;必填:Y;类型长度:int,1;默认:0;
                $macrovision = 0;
                // 节点名:Description(节目描述);必填:Y;类型长度:string,2048;
                $description = '';
                // 节点名:PriceTaxIn(列表定价);必填:N;类型长度:float,12,2;
                $priceTaxIn = '';
                // 节点名:Status(节目状态标志);必填:N;类型长度:int,1;默认:1;
                $status = 1;
                // 节点名:SourceType;取值:1=>VDO,5=>Advertisment;必填:Y;类型长度:int,1;默认:1;
                $sourceType = '';
                // 节点名:SeriesFlag;取值:0=>普通VDO,1=>连续剧剧集;必填:Y;int,1;
                $type = '';
                // 节点名:Keywords(关键字);必填:Y;类型长度:string,1024;多个关键字使用/分隔;
                $keywords = '';
                // 节点名:Tags(标签集合);必填:Y;类型长度:string,1024;多个标签使用/分隔;
                $tags = '';
                // 节点名:ContentProvider(内容提供商);必填:N;类型长度:string,64;
                $contentProvider = '';
                // 节点名:Duration(节目时长);必填:Y;类型长度:int,3;以分钟为单位;
                $duration = '';
                // 节点名:PlayCount(播放次数);必填:N;类型长度:int,6;
                $playCounts = '';
                // 节点名:Score(评分);必填:N;类型长度:int,6;
                $score = $vseriali->score;
                // 节点名:WatchFocus(看点);必填:N;类型长度:int,6;
                $watchFocus = '';
                // 节点名:CPContentID(CP标识);必填:Y;类型长度:String,128;
                $cpContentID = '';

                // 单个<Object>
                $object = $dom->createElement('Object');
                // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                // $no3 = $dom->createAttribute('id');//创建属性节点：id
                // $no3->value = 1;//给属性节点赋值
                // $no2->appendChild($no3);//把属性节点加入到book节点中
                $object->setAttribute('ElementType', $elementType);
                // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                // 唯一ID, 必须
                $object->setAttribute('ID', $program_id);
                // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                $object->setAttribute('Action', $action);
                // 全局唯一标志, 必须
                $object->setAttribute('Code', $program_code);
                $objects->appendChild($object);

                // 视频名称<Property>
                $property = $dom->createElement('Property');
                // 视频名称, 必须
                $property->setAttribute('Name', 'Name');
                $text = $dom->createTextNode($name);
                $property->appendChild($text);
                $object->appendChild($property);

                // 节目订购编号<Property>
                $property = $dom->createElement('Property');
                // 节目订购编号, 非必须
                $property->setAttribute('Name', 'OrderNumber');
                $text = $dom->createTextNode($orderNumber);
                $property->appendChild($text);
                $object->appendChild($property);

                // 原名<Property>
                $property = $dom->createElement('Property');
                // 原名, 非必须
                $property->setAttribute('Name', 'OriginalName');
                $text = $dom->createTextNode($originalName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 剧集排序规则<Property>
                $property = $dom->createElement('Property');
                // 剧集排序规则, 0降序, 1升序, 必须
                $property->setAttribute('Name', 'SortName');
                $text = $dom->createTextNode($sortName);
                $property->appendChild($text);
                $object->appendChild($property);

                // 搜索简拼<Property>
                $property = $dom->createElement('Property');
                // 搜索简拼, 非必须
                $property->setAttribute('Name', 'KeyWords');
                $text = $dom->createTextNode($keyWords);
                $property->appendChild($text);
                $object->appendChild($property);

                // 演员列表<Property>
                $property = $dom->createElement('Property');
                // 演员列表, 必须
                $property->setAttribute('Name', 'ActorDisplay');
                $text = $dom->createTextNode($actors);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 导演列表, 必须
                $property->setAttribute('Name', 'WriterDisplay');
                $text = $dom->createTextNode($directors);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 国家, 必须
                $property->setAttribute('Name', 'OriginalCountry');
                $text = $dom->createTextNode($vareas);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 语言, 非必须
                $property->setAttribute('Name', 'Language');
                $text = $dom->createTextNode($vlangs);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 上映年份, 必须
                $property->setAttribute('Name', 'ReleaseYear');
                $text = $dom->createTextNode($vyears);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 首播日期, 必须
                $property->setAttribute('Name', 'OrgAirDate');
                $text = $dom->createTextNode($orgairDate);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效开始时间, 必须
                $property->setAttribute('Name', 'LicensingWindowStart');
                $text = $dom->createTextNode($licensingWindowStart);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 有效结束时间, 必须
                $property->setAttribute('Name', 'LicensingWindowEnd');
                $text = $dom->createTextNode($licensingWindowEnd);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 拷贝保护标志, 必须
                $property->setAttribute('Name', 'Macrovision');
                $text = $dom->createTextNode($macrovision);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目描述, 必须
                $property->setAttribute('Name', 'Description');
                $text = $dom->createTextNode($description);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 列表定价, 不必须
                $property->setAttribute('Name', 'PriceTaxIn');
                $text = $dom->createTextNode($priceTaxIn);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目状态标志, 必须
                $property->setAttribute('Name', 'Status');
                $text = $dom->createTextNode($status);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // VOD/Adverisement, 必须
                $property->setAttribute('Name', 'SourceType');
                $text = $dom->createTextNode($sourceType);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目内容类型, 必须
                $property->setAttribute('Name', 'Type');
                $text = $dom->createTextNode($type);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 关键字, 多个关键字直接用/
                $property->setAttribute('Name', 'Keywords');
                $text = $dom->createTextNode($keywords);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 标签集合, 必须
                $property->setAttribute('Name', 'Tags');
                $text = $dom->createTextNode($tags);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 内容提供商, 非必须
                $property->setAttribute('Name', 'ContentProvider');
                $text = $dom->createTextNode($contentProvider);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 节目时长, 必须
                $property->setAttribute('Name', 'Duration');
                $text = $dom->createTextNode($duration);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 播放次数, 非必须
                $property->setAttribute('Name', 'PlayCount');
                $text = $dom->createTextNode($playCounts);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 评分, 非必须
                $property->setAttribute('Name', 'Score');
                $text = $dom->createTextNode($score);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // 看点, 非必须
                $property->setAttribute('Name', 'WatchFocus');
                $text = $dom->createTextNode($watchFocus);
                $property->appendChild($text);
                $object->appendChild($property);

                // <Property>
                $property = $dom->createElement('Property');
                // CP标识, 必须
                $property->setAttribute('Name', 'CPContentID');
                $text = $dom->createTextNode($cpContentID);
                $property->appendChild($text);
                $object->appendChild($property);
                /**
                 * ElementType="Program" end
                 */

                /**
                 * ElementType="Movie" start
                 */
                $elementType = 'Movie';
                $vifiles     = $vseriali->vifiles;
                // 存放文件的id和code
                $movies = [];
                foreach ($vifiles as $key => $value) {
                    // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                    $movie_id = str_pad(makeRandomNumber(32), 32, "0", STR_PAD_LEFT);
                    $movies[] = $movie_id;
                    // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                    $movie_action = 'REGIST';
                    // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                    $movie_code = $movie_id;
                    // 属性名:FileURL(媒体文件URL);取值:1=>正片, 2:花絮/短片, 3=>预告片, 4=>回看点播;必填:Y;类型长度:int,1;
                    $movie_type = $value->type;
                    // 属性名:FileURL(媒体文件URL);必填:Y;类型长度:String,1024;
                    $movie_path = $value->path;
                    // 属性名:SourceDRMType;取值:0=>No DRM, 1=>BES DRM;必填:N;类型长度:int,1;默认为:0;
                    $sourceDRMType = '';
                    // 属性名:DestDRMType;取值:0=>No DRM, 1=>BES DRM;必填:N;类型长度:int,1;默认为:0;
                    $destDRMType = '';
                    // 属性名:AudioType;取值:0=>其他, 1=>Monaural(单声道), 2=>Stereo(多声道), 3=>Two-nation monaural(双声道), 4=>Two-nation stereo(双多声道);必填:N;类型长度:int,2;默认为:1;
                    $audioType = '';
                    // 属性名:ScreenFormat;取值:0=>4*3, 1=>16*9(Wide), 2=>无;必填:N;类型长度:int,1;默认为:2;
                    $screenFormat = '';
                    // 属性名:ClosedCaptioning(字幕标志);取值:0=>无字幕, 1=>有字幕;必填:N;类型长度:int,1;默认为:1;
                    $closedCaptioning = '';
                    // 属性名:Duration(播放时长);必填:N;类型长度:int,3;以秒计单位,当type=4时,为回看时长;
                    $duration = $value->duration;
                    // 属性名:FileSize(文件大小);必填:N;类型长度:string,16;单位为byte;
                    $size = (int) $value->size * 1024 * 1024;
                    // 属性名:BitRateType(码率);必填:Y;类型长度:int,4;单位为kbps;
                    $bitRateType = '';
                    // 属性名:Resolution(分辨率);必填:N;类型长度:string,4;
                    $resolutionId = ''; //$value->resolution_id;
                    // 视频类型
                    $mediaMode = '';
                    // 属性名:MediaType(编码格式);取值:1=>H.264, 2=>MPEG4, 3=>AVS, 4=>MPEG2, 5=>MP3, 6=>WMV;必填:N;默认为:1;
                    $mediaType = '';
                    // 属性名:SystemLayer;取值:1=>TS, 2=>3GP, 3=>flv, 4=>MP4, 5=>wmv;必填:Y;类型长度:int,1;
                    $vfileextId = 1; //$value->vfileext_id;
                    // 属性名:ServiceType(服务类型);取值:1=>在线播放(默认), 2=>支持下载, 3=>在线播放+下载;必填:Y;类型长度:int,1;默认为:1;
                    $serviceType = '';
                    // 属性名:showType(展示类型);取值:1=>2D, 2=>3D, 3=>左右3D, 4=>上下3D;必填:Y;类型长度:int,1;
                    $showType = '';
                    // 属性名:CPContentID(CP标识);必填:N;类型长度:string,128;
                    $cpContentID = '';
                    // mediaProto
                    $mediaProto = '';
                    // 属性名:LiveId(回看点播源频道ID);必填:N;类型长度:string,128;当Type类为4时,此字段必传;
                    $liveId = '';
                    // 属性名:BeginTime(回看源开始时间);必填:N;类型长度:string,128;当Type类为4时,此字段必传;
                    $beginTime = '';

                    // 单个<Object>
                    $object = $dom->createElement('Object');
                    // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                    // $no3 = $dom->createAttribute('id');//创建属性节点：id
                    // $no3->value = 1;//给属性节点赋值
                    // $no2->appendChild($no3);//把属性节点加入到book节点中
                    $object->setAttribute('ElementType', $elementType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $object->setAttribute('ID', $movie_id);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $object->setAttribute('Action', $movie_action);
                    // 全局唯一标志, 必须
                    $object->setAttribute('Code', $movie_code);
                    $objects->appendChild($object);

                    // 媒体类型<Property>
                    $property = $dom->createElement('Property');
                    // 媒体类型, 必须
                    $property->setAttribute('Name', 'Type');
                    $text = $dom->createTextNode($movie_type);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 媒体文件URL<Property>
                    $property = $dom->createElement('Property');
                    // 媒体文件URL, 必须
                    $property->setAttribute('Name', 'FileURL');
                    // $real_movie_path = 'ftp://' . $ftp_config['username'] . ':' . $ftp_config['password'] . '@' . $ftp_config['host'] . '/' . $movie_path;
                    // $real_movie_path = 'ftp://hdty:hdty12345@ftp.am.jmxxb:21/' . $movie_path;
                    $real_movie_path = 'ftp://' . config('custom.base.media_injection.ts_ftp_username') . ':' . config('custom.base.media_injection.ts_ftp_password') . '@' . config('custom.base.media_injection.ts_ftp_domain') . '/' . $movie_path;
                    $text            = $dom->createTextNode($real_movie_path);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 视频名称<Property>
                    $property = $dom->createElement('Property');
                    // 视频名称, 必须
                    $property->setAttribute('Name', 'Name');
                    $text = $dom->createTextNode($name);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // SourceDRMType<Property>
                    $property = $dom->createElement('Property');
                    // SourceDRMType, 非必须
                    $property->setAttribute('Name', 'SourceDRMType');
                    $text = $dom->createTextNode($sourceDRMType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // DestDRMType<Property>
                    $property = $dom->createElement('Property');
                    // DestDRMType, 非必须
                    $property->setAttribute('Name', 'DestDRMType');
                    $text = $dom->createTextNode($destDRMType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 声道类型<Property>
                    $property = $dom->createElement('Property');
                    // 声道类型, 非必须
                    $property->setAttribute('Name', 'AudioType');
                    $text = $dom->createTextNode($audioType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // ScreenFormat<Property>
                    $property = $dom->createElement('Property');
                    // ScreenFormat, 非必须
                    $property->setAttribute('Name', 'ScreenFormat');
                    $text = $dom->createTextNode($screenFormat);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 字幕标志<Property>
                    $property = $dom->createElement('Property');
                    // 字幕标志, 非必须
                    $property->setAttribute('Name', 'ClosedCaptioning');
                    $text = $dom->createTextNode($closedCaptioning);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 播放时长<Property>
                    $property = $dom->createElement('Property');
                    // 播放时长, 非必须
                    $property->setAttribute('Name', 'Duration');
                    $text = $dom->createTextNode($duration);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 文件大小<Property>
                    $property = $dom->createElement('Property');
                    // 文件大小, 非必须
                    $property->setAttribute('Name', 'FileSize');
                    $text = $dom->createTextNode($size);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 码率<Property>
                    $property = $dom->createElement('Property');
                    // 码率, 非必须
                    $property->setAttribute('Name', 'BitRateType');
                    $text = $dom->createTextNode($bitRateType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 编码格式<Property>
                    $property = $dom->createElement('Property');
                    // 编码格式, 必须
                    $property->setAttribute('Name', 'MediaType');
                    $text = $dom->createTextNode($mediaType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 分辨率<Property>
                    $property = $dom->createElement('Property');
                    // 分辨率, 非必须
                    $property->setAttribute('Name', 'Resolution');
                    $text = $dom->createTextNode($resolutionId);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 视频类型<Property>
                    $property = $dom->createElement('Property');
                    // 视频类型, 非必须
                    $property->setAttribute('Name', 'MediaMode');
                    $text = $dom->createTextNode($mediaMode);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 文件扩展名<Property>
                    $property = $dom->createElement('Property');
                    // 文件扩展名, 必须
                    $property->setAttribute('Name', 'SystemLayer');
                    $text = $dom->createTextNode($vfileextId);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 服务类型<Property>
                    $property = $dom->createElement('Property');
                    // 服务类型, 必须
                    $property->setAttribute('Name', 'ServiceType');
                    $text = $dom->createTextNode($serviceType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 展示类型<Property>
                    $property = $dom->createElement('Property');
                    // 展示类型, 必须
                    $property->setAttribute('Name', 'ShowType');
                    $text = $dom->createTextNode($showType);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // <Property>
                    $property = $dom->createElement('Property');
                    // mediaProto, 必须
                    $property->setAttribute('Name', 'mediaProto');
                    $text = $dom->createTextNode($mediaProto);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // <Property>
                    $property = $dom->createElement('Property');
                    // LiveId, 必须
                    $property->setAttribute('Name', 'LiveId');
                    $text = $dom->createTextNode($liveId);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // <Property>
                    $property = $dom->createElement('Property');
                    // BeginTime, 必须
                    $property->setAttribute('Name', 'BeginTime');
                    $text = $dom->createTextNode($beginTime);
                    $property->appendChild($text);
                    $object->appendChild($property);
                }
                /**
                 * ElementType="Movie" end
                 */

                /**
                 * ElementType="Picture" start
                 */
                $elementType = 'Picture';
                $vposteris   = $vseriali->vposteris;
                // 存放图片的id和code
                $pictures = [];
                foreach ($vposteris as $key => $value) {
                    // 属性名:ID(唯一ID);必填:Y;类型长度:32;
                    $picture_id = str_pad(makeRandomNumber(32), 32, "0", STR_PAD_LEFT);
                    $pictures[] = $picture_id;
                    // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                    $picture_action = 'REGIST';
                    // 属性名:Code(全局唯一标志);必填:Y;类型长度:128;
                    $picture_code = $picture_id;
                    // 属性名:FileURL(图片文件URL);类型长度:string,1024;此路径支持ftp、http两种格式
                    $picture_href = $value->href;
                    // 属性名:Description(描述);类型长度:string,1024;
                    $picture_description = $value->name;
                    // 属性名:Type(类型);取值:0=>大图, 1=>中图, 2=>小图, 3=>竖图, 4=>横图, 5=>方图, 6=>背景图, 7=>栏目图片
                    $picture_type = ($value->imgtpl_id - 1);

                    // 单个<Object>
                    $object = $dom->createElement('Object');
                    // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                    // $no3 = $dom->createAttribute('id');//创建属性节点：id
                    // $no3->value = 1;//给属性节点赋值
                    // $no2->appendChild($no3);//把属性节点加入到book节点中
                    $object->setAttribute('ElementType', $elementType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $object->setAttribute('ID', $picture_id);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $object->setAttribute('Action', $picture_action);
                    // 全局唯一标志, 必须
                    $object->setAttribute('Code', $picture_code);
                    $objects->appendChild($object);

                    // 图片文件URL<Property>
                    $property = $dom->createElement('Property');
                    // 图片文件URL
                    $property->setAttribute('Name', 'FileURL');
                    $real_picture_href = 'ftp://' . $ftp_config['username'] . ':' . $ftp_config['password'] . '@' . $ftp_config['host'] . '/' . $picture_href;
                    $text              = $dom->createTextNode($real_picture_href);
                    $property->appendChild($text);
                    $object->appendChild($property);

                    // 图片类型<Property>
                    $property = $dom->createElement('Property');
                    // 图片类型
                    $property->setAttribute('Name', 'Type');
                    $text = $dom->createTextNode($picture_type);
                    $property->appendChild($text);
                    $object->appendChild($property);
                }
                /**
                 * ElementType="Picture" end
                 */

                /**
                 * ElementType="Mappings" start
                 */
                // 建立<Mappings>元素并将其作为<ADI>的子元素
                $mappings = $dom->createElement('Mappings');
                $adi->appendChild($mappings);

                // 单个<Object>
                $mapping = $dom->createElement('Mapping');
                // Series-连续剧剧头信息|Program-节目信息|Movie-媒体内容信息|Channel-频道信息|PhysicalChannel-频道物理参数信息|Schedule-频道节目编排信息|Cast-人物信息|Picture-图片信息|Category-编排目录信息, 必须
                // $no3 = $dom->createAttribute('id');//创建属性节点：id
                // $no3->value = 1;//给属性节点赋值
                // $no2->appendChild($no3);//把属性节点加入到book节点中
                $partentType = 'Series';
                $elementType = 'Program';
                // 属性名:Action(操作类型);取值:发布=>REGIST, 更新=>UPDATE, 删除=>DELETE;必填:Y;
                $action = 'REGIST';
                // 属性名:PartentID(父元素ID);必填:Y;
                // Series
                $parentId   = $id;
                $parentCode = $code;
                // Program
                $elementId   = $program_id;
                $elementCode = $program_code;
                $mapping->setAttribute('ParentType', $partentType);
                // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                // 唯一ID, 必须
                $mapping->setAttribute('ParentID', $parentId);
                $mapping->setAttribute('ElementType', $elementType);
                $mapping->setAttribute('ElementID', $elementId);
                // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                $mapping->setAttribute('Action', $action);
                // 全局唯一标志, 必须
                $mapping->setAttribute('ParentCode', $parentCode);
                $mapping->setAttribute('ElementCode', $elementCode);
                $mappings->appendChild($mapping);
                // <Property>
                $property = $dom->createElement('Property');
                $property->setAttribute('Name', 'Sequence');
                $text = $dom->createTextNode(0);
                $property->appendChild($text);
                $mapping->appendChild($property);
                // 遍历文件信息
                foreach ($movies as $value) {
                    $mapping = $dom->createElement('Mapping');
                    $mappings->appendChild($mapping);
                    $elementType = 'Movie';
                    $partentType = 'Program';
                    $action      = 'REGIST';
                    $parentCode  = '';
                    $elementCode = '';

                    $mapping->setAttribute('ParentType', $partentType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $mapping->setAttribute('ParentID', $program_id);
                    $mapping->setAttribute('ElementType', $elementType);
                    $mapping->setAttribute('ElementID', $value);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $mapping->setAttribute('Action', $action);
                    // 全局唯一标志, 必须
                    $mapping->setAttribute('ParentCode', $program_id);
                    $mapping->setAttribute('ElementCode', $value);
                    // $mappings->appendChild($mapping);
                }
                // 遍历图片信息
                foreach ($pictures as $value) {
                    $mapping = $dom->createElement('Mapping');
                    $mappings->appendChild($mapping);
                    $elementType = 'Series';
                    $partentType = 'Picture';
                    $action      = 'REGIST';
                    $type        = 0;
                    $mapping->setAttribute('ParentType', $partentType);
                    // $no4 = $dom->createCDATASection('天龙八部是金庸写的一本武侠小说，非常好看！');//创建文CDATA节点
                    // 唯一ID, 必须
                    $mapping->setAttribute('ParentID', $value);
                    $mapping->setAttribute('ElementType', $elementType);
                    $mapping->setAttribute('ElementID', $id);
                    // 全局唯一标志, 必须
                    $mapping->setAttribute('ParentCode', $value);
                    $mapping->setAttribute('ElementCode', $code);
                    // 发布-REGIST, 更新-UPDATE, 删除-DELETE, 必须
                    $mapping->setAttribute('Action', $action);

                    // <Property>
                    $property = $dom->createElement('Property');
                    // Sequence
                    $property->setAttribute('Name', 'Sequence');
                    $text = $dom->createTextNode(90);
                    $property->appendChild($text);
                    $mapping->appendChild($property);

                    // 图片类型<Property>
                    $property = $dom->createElement('Property');
                    // 图片类型, 必须
                    $property->setAttribute('Name', 'Type');
                    $text = $dom->createTextNode($type);
                    $property->appendChild($text);
                    $mapping->appendChild($property);
                }
                /**
                 * ElementType="Mappings" end
                 */

                // echo $dom->saveXML();
                // die();
                // 保存到xml
                // 完整路径
                $fullPath1 = $fullPath . 'vserialis' . $vseriali_id . '.xml';
                $bool      = Storage::disk($driver)->put($fullPath1, $dom->saveXML());
                if ($bool) {
                    /**
                     * 发起注入请求start
                     */
                    /**
                     *
                     * 内容下发
                     * 消息方向:CSP->LSP, LSP以 `cmspost` 接受XML指令
                     * 通知响应: Result:0成功, -1 失败; ErrorDescription:错误信息详细描述
                     * 下游接受消息, 并同步反馈接受结果
                     */
                    $time   = udate('YmdHisu');
                    // 第一位 `f` 表示分集, 第一个 `j` 表示剧集
                    $msg_id = 'f' . $time . uniqid() . $vseriali_id;
                    // cpid
                    $cp_id    = $config_media_injection_cp_id;
                    // 注入调用的地址
                    $url = $config_media_injection_url;
                    // xmld地址
                    // $file_url = 'ftp://web1:123456@10.69.42.148/upload/xml/3.xml';
                    $file_url = 'ftp://' . $ftp_config['username'] . ':' . $ftp_config['password'] . '@' . $ftp_config['host'] . '/' . $fullPath1;

                    // 定义content-type为xml
                    $header[] = "Content-type: text/xml";
                    // 构造xml
                    /*$post_data = '<?xml version="1.0" encoding="UTF-8"?>';*/
                    // $data = [];
                    $post_data = '';
                    $post_data .= '<xmlpost>';
                    $post_data .= '<time>' . $time . '</time>';
                    $post_data .= '<msgid>' . $msg_id . '</msgid>';
                    $post_data .= '<cp_id>' . $cp_id . '</cp_id>';
                    $post_data .= '<url>' . $file_url . '</url>';
                    $post_data .= '</xmlpost>';
                    // $data['cmspost'] = $post_data;
                    // 首先检测是否支持curl
                    if (!extension_loaded("curl")) {
                        trigger_error("对不起，请开启curl功能模块！", E_USER_ERROR);
                    }

                    // 初始一个curl会话
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
                    // 设置url
                    curl_setopt($ch, CURLOPT_URL, $url);
                    // TRUE, 将curl_exec()获取的信息以字符串返回, 而不是直接输出
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    // 超时时间
                    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
                    // curl如果需要进行毫秒超时
                    // curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);
                    // 或者curl_setopt ( $ch,  CURLOPT_NOSIGNAL,true);//支持毫秒级别超时设置
                    // 设置发送方式:post
                    curl_setopt($ch, CURLOPT_POST, 1);
                    // 设置发送数据
                    // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                    // 执行cURL会话
                    $response = curl_exec($ch);
                    if (curl_errno($ch)) {
                        print curl_error($ch);
                    }
                    curl_close($ch);
                    // post的数据为xml字符串，通过 $xml = simplexml_load_string($post_data);转换成xml对象
                    $response_xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
                    // 把simplexml对象转换成 json，再将 json 转换成数组。
                    $response_array = $value_array = json_decode(json_encode($response_xml), true);
                    if ($response_array && $response_array['@attributes'] && $response_array['@attributes']['ret'] === '0') {
                        Vseriali::where('id', '=', $vseriali_id)->update(['msg_id' => $msg_id, 'sync_result' => 1]);
                    }

                    /**
                     * 发起注入请求end
                     */

                    // 注入媒资总数
                    $injectionVasseticount++;
                } else {
                    // 错误的媒资id
                    $errorRow[] = $vseriali_id;
                }
            }
        // });

        // 结束时间
        $endtime        = microtime(true);
        $consuming_time = round($endtime - $starttime, 4);
        $endMemory      = memory_get_usage();
        $usedMemory     = ($endMemory - $startMemory) / 1024;

        $flag       = true;
        $errorCount = count($errorRow);
        $message    = '<div style="word-wrap: break-word;">';
        $message .= '耗时:';
        $message .= $consuming_time . '秒<br />';
        $message .= '耗费内存:';
        $message .= "{$usedMemory} K<br />";
        $message .= '注入总数:';
        $message .= $this->injectVserialiHistory->count . '<br />';
        $message .= '注入成功数:';
        $message .= $injectionVasseticount . '<br />';
        $message .= '注入失败数:';
        $message .= count($errorRow) . '<br />';
        if ($errorCount > 0) {
            $flag = false;
            $message .= '注入失败的分集媒资id:' . implode(',', $errorRow) . '<br />';
        }
        $message .= '</div>';
        //通过标志位判断前端返回时成功还是失败
        if ($flag) {
            $message = '<div class="alert alert-success">' . $message . '</div>';
        } else {
            $message = '<div class="alert alert-error">' . $message . '</div>';
        }

        $this->injectVserialiHistory->consuming_time = $consuming_time;
        $this->injectVserialiHistory->used_memory    = $usedMemory;
        $this->injectVserialiHistory->content        = $message;
        $this->injectVserialiHistory->save();
        return $message;
    }
}
